﻿package com.dcliment.jukebox.controllers
{
	import com.dcliment.controllers.MetricsController;
	import com.dcliment.display.gui.SimplePPButton;
	import com.dcliment.elements.MetricsElement;
	import com.dcliment.jukebox.display.views.TimerView;
	import com.dcliment.jukebox.events.JukeBoxEvent;
	import com.dcliment.jukebox.models.JukeBoxModel;
	import com.dcliment.jukebox.models.TimerModel;
	import com.dcliment.utils.CustomTimer;
	import com.dcliment.utils.DisplayObjectUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	
	
	/**
	 *
	 *  
	 * @author dcliment
	 * version 1.1;
	 */	
	public class TimerController extends EventDispatcher
	{
		private var _jukeBoxModel:JukeBoxModel;
		private var _jukeBoxController:JukeBoxController;
		private var _view:Sprite;
		private var _timerView:TimerView;
		private var _tModel:TimerModel;
		// Timers
		private var _timer:CustomTimer;
		private var _pauseTimer:CustomTimer;
		// play pause button metrics element
		private var _metricsController:MetricsController;
		private var _ppMetrics:MetricsElement;
		// Display
		private var _ppButton:SimplePPButton;
		
		public function TimerController($model:JukeBoxModel, $view:Sprite, $controller:JukeBoxController)
		{
			_jukeBoxModel 		= $model;
			_jukeBoxController 	= $controller;
			_view 				= $view;
			
			_jukeBoxModel.addEventListener(JukeBoxEvent.ID_CHANGE, onIDChange, false, 0, true);
			
			_timer = new CustomTimer(NaN, onTimerComplete, 0); // the key is to set the repeat to 0 so it repeats constantly
			_timer.addEventListener(CustomTimer.TIMER, updateTimer, false, 0, true);
			
		}
		
		public function setup($timerModel:TimerModel):void
		{
			_tModel = $timerModel;
			
			// Metrics
			_ppMetrics = new MetricsElement();
			_ppMetrics.type = MetricsElement.EVENT;
			_ppMetrics.token = _tModel.ppButtonToken;
			
			// pause timer
			_pauseTimer = new CustomTimer(_tModel.pauseTimerDelay, onPauseTimerComplete);
			
			// VIEWS
			_view.x = $timerModel.posX;
			_view.y = $timerModel.posY;
			_view.visible = _tModel.showTimerView;
			
			
			// Display
			_timerView = DisplayObjectUtil.getLibraryObject("TimerBar") as TimerView;
			_ppButton = _timerView.playPauseButton;
			_ppButton.addEventListener(SimplePPButton.PAUSE_CLICKED, onPauseClicked, false, 0, true);
			_ppButton.addEventListener(SimplePPButton.PLAY_CLICKED, onPlayClicked, false, 0, true);
			
			_view.addChild(_timerView);
			
			
		}
		// -----------------------------------
		// pause timer ~~ the timer that starts when pause is clicked 
		private function onPauseTimerComplete():void
		{
			this.startTimer();
		}
		private function startPauseTimer():void
		{
			_pauseTimer.restart();
		}
		private function pausePauseTimer():void
		{
			_pauseTimer.pause();
		}
		// -----------------------------------
		// TIMER functions
		private function onTimerComplete():void
		{
			_jukeBoxController.nextPage();
			_timer.pause();
		}
		private function updateTimer(e:Event):void
		{				
			_timerView.time = _timer.lastingTime;
		}
		private function startTimer():void
		{	
			_timer.start();
			_timerView.playState();
			//
			this.pausePauseTimer();
			//
			_jukeBoxModel.playState = true;
			
		}
		private function pauseTimer():void
		{
			_timer.pause();
			_timerView.pauseState();
			//
			this.startPauseTimer();
			//
			_jukeBoxModel.playState = false;
		}
		private function resetTimer():void
		{
			//_timer.restart();
			_timer.pause();
			_timer.reset();
			
		}
		// --------------------------------------
		// EVENT Listeners
		//
		private function onIDChange(e:JukeBoxEvent):void
		{
			//this.resetTimer();// if you reset it goes to :: 0
			
			_timer.delay = _jukeBoxController.time * 1000;
			
			_timerView.time = _timer.delay;
			
			// if paused then reset TIMER()
			if(_jukeBoxModel.playState == false)
				this.resetTimer();
			
			this.startTimer();
		
		}
		// Play Pause Button Click events
		private function onPauseClicked(e:Event):void
		{
			// first metrics
			_ppMetrics.objectState = _tModel.ppObjectStateOff;
			_jukeBoxController.track(_ppMetrics);
			// second send to appModel playState so contentController can do its thing
			pauseTimer();
		}
		private function onPlayClicked(e:Event):void
		{
			
			// first metrics
			_ppMetrics.objectState = _tModel.ppObjectStateOn;
			_jukeBoxController.track(_ppMetrics);
			// second send to appmodel playstate so ContentController can do its thing
			startTimer();
			
		}
		//------------------------------------------------------
		// Public functions
		public function pauseController():void
		{
			_ppButton.playState();
			
			pauseTimer();
		}
		public function playController():void
		{
			_ppButton.pauseState();
			
			startTimer();
		}
		public function resetPauseTimer():void
		{
			_pauseTimer.restart();
		}
	}
}